package com.qf.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 自定义
 * 1、打日志
 * 2、记录时间
 * 3、记录用户行为
 * ....
 *
 * @author lixu
 */
@Aspect //当前此类为切面实现类 代理对象
@Component //通用注解 ： 实例化当前对象 将当前对象加入到SpringIOC容器中
public class MyAopAspect {

    //切入点  表达式 公共提取
    @Pointcut(value = "execution(* com.qf.service.EmpServiceImpl.*(..))")
    public void pointCut(){}

    //前置通知
    @Before(value = "pointCut() && args(name, age)",argNames = "name,age")
    public void beforeAddUser(String name, Integer age) {
        System.out.println("===自定义前置通知==:参数:name =" +  name + ", age = " + age);
    }
    //执行方法  public List<Emp> add
    //后置通知
    @AfterReturning(value = "pointCut()",returning = "result")
    public void afterReturning(Object result){
        System.out.println("===自定义后置通知==:返回值:" +  result);
    }
    //异常通知
    @AfterThrowing(value = "pointCut()",throwing = "e")
    public void afterReturning(Exception e){
        System.out.println("===自定义异常通知==:异常是:" +  e.getMessage());
    }
    //环绕通知
    //@Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint pjp){
        System.out.println("===自定义环绕通知==");
        Object result = null;
        //前置相关代码
        try {
            result = pjp.proceed();
        } catch (Throwable e) {
            //处理异常
            return result;
        }
        //后置代码
        return result;
    }








}
